/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webbucks;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author scartissue
 */
public class TheServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
    
    private static String jdbcURL = "jdbc:mysql://localhost:8889/webbucks";
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession sess = request.getSession();
        sess.setAttribute("currentUser", request.getRemoteUser());
        sess.setAttribute("jdbcURL",jdbcURL);
        RequestDispatcher rd = null;
        
        // find out what to do based on the attribute "action"
	// no action or show

        if(request.getParameter("action") == null || 
           request.getParameter("action").equals("show")){
	    
            // A request dispatcher that's connected to the page.
	    
            rd = request.getRequestDispatcher("http://localhost:8080/webbucks-war/index.jsp"); 
            rd.forward(request,response);
        }
        
        // create an empty profile, store in the in session
	// with all available roles

	else if(request.getParameter("action").equals("newuser")) {
	  //  CustomerBean p = new UserBean(jdbcURL);
	    try {
             // HashMap<String,Boolean> role = p.getRoles();
	      //  sess.setAttribute("roles",role);
             }
            catch(Exception e) {
                throw new ServletException("Error loading profile", e);
            }
            
        //  sess.setAttribute("profile", p);

	    //rd = request.getRequestDispatcher(userPage);
	    rd.forward(request, response);
        }
        
   
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
